package judesart.app.client.ui.admin.project;

import java.util.List;

import judesart.app.client.controller.ProjectController;
import judesart.app.client.controller.ProjectControllerAsync;
import judesart.app.domain.Project;
import judesart.app.shared.ArtworkShort;

import com.allen_sauer.gwt.log.client.Log;
import com.extjs.gxt.ui.client.data.BaseListLoadConfig;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ArtworksPanel extends Composite
{
    ProjectControllerAsync service = GWT.create(ProjectController.class);
    private final LayoutContainer artworksLayoutContainer;
    private Project project;

    private final ListLoader<ListLoadResult<ArtworkShort>> loader;

    public ArtworksPanel(Project project)
    {
        setProject(project);
        artworksLayoutContainer = new LayoutContainer();
        artworksLayoutContainer.setWidth(798);
        artworksLayoutContainer.setAutoHeight(true);
        artworksLayoutContainer.setBorders(false);

        RpcProxy<List<ArtworkShort>> proxy = new RpcProxy<List<ArtworkShort>>()
        {
            @Override
            protected void load(Object loadConfig,
                    AsyncCallback<List<ArtworkShort>> callback)
            {
                Log.debug(" >> load artworks ");
                service.findProjectArtworksImages(getProject().getId(),
                        callback);
            }
        };

        loader = new BaseListLoader<ListLoadResult<ArtworkShort>>(proxy,
                new BeanModelReader());
        ListStore<BeanModel> store = new ListStore<BeanModel>(loader);
        loader.load();

        final ListView<BeanModel> artworkListView = new ListView<BeanModel>(
                store);
        artworkListView.setLoadingText("loading...");
        artworkListView.setBorders(false);
        artworkListView.setSize("796", "420");
        artworkListView.setTemplate(getTemplate());
        artworkListView.setItemSelector("div.thumb-wrap");
        artworkListView.getSelectionModel().addListener(Events.SelectionChange,
                new Listener<SelectionChangedEvent<BaseModel>>()
                {
                    public void handleEvent(SelectionChangedEvent<BaseModel> be)
                    {
                        onSelectionChanged(be);
                    }
                });

        artworksLayoutContainer.add(artworkListView);

        initComponent(artworksLayoutContainer);
    }

    protected Project getProject()
    {
        return project;
    }

    protected void setProject(Project project)
    {
        this.project = project;
    }

    protected void openProject(Project project)
    {
        setProject(project);
        loader.load(new BaseListLoadConfig());
    }

    private native String getTemplate() /*-{
        return ['<tpl for=".">', 
        '<div style="border: 1px solid #FFFFFF;float:left;margin:4px 0 4px  4px; padding:2px;width:244px; height: 300px;" class="thumb-wrap" id="{id}">', 
        '<div style="width: 240px;" class="thumb"><img src="/image?id={imageId}&size=240x240"></div>', 
        '<div style="width: 100%; text-align: center; color: #999999;">{title}</div>',
        '</div>',
        '</tpl>', 
        '<div class="x-clear"></div>'].join("");
    }-*/;

    private void onSelectionChanged(SelectionChangedEvent<BaseModel> be)
    {
        Object source = be.getSource();
        Log.debug("source [" + source + "]");
        if (be.getSelection().size() == 0)
        {
            return;
        }
        final BaseModel model = be.getSelection().get(0);
    }

}
